/*
 *  Copyright (C) 2010 Francesco Boffa (aka AlfaOmega08)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ImageFormats.Iso9660;

import java.io.*;

public class DirectoryRecord {
	private byte Size;
	private byte ExtRecordSectors;
	private BothEndianDWord FirstSectorOfData;
	private BothEndianDWord LengthOfData;
	private byte Year;	// Since 1900
	private byte Month;	// 1 = january
	private byte Day;	// 1 - 31
	private byte Hour;	// 0 - 23
	private byte Minute;	// 0 - 59
	private byte Second;	// 0 - 59
	private byte GmtOffset;
	private byte Flags;
	private byte FileSizeInterleaved;
	private byte InterleavedGapForFile;
	private BothEndianWord VolumeSequenceNumber;
	private byte NameLength;
	private String Name;

	public void getFromStream(RandomAccessFile f) {
		try {
			long Fp = f.getFilePointer();

			Size = f.readByte();
			if (Size % 2 != 0) {
				System.out.println("Directory entry size is odd!");
				System.exit(-1);
			}

			byte[] tmp = new byte[8];

			ExtRecordSectors = f.readByte();

			f.read(tmp, 0, 8);
			FirstSectorOfData = new BothEndianDWord(tmp);
			f.read(tmp, 0, 8);
			LengthOfData = new BothEndianDWord(tmp);

			Year = f.readByte();
			Month = f.readByte();
			Day = f.readByte();
			Hour = f.readByte();
			Minute = f.readByte();
			Second = f.readByte();
			GmtOffset = f.readByte();

			Flags = f.readByte();
			FileSizeInterleaved = f.readByte();
			InterleavedGapForFile = f.readByte();

			f.read(tmp, 0, 4);
			VolumeSequenceNumber = new BothEndianWord(tmp);
			NameLength = f.readByte();

			tmp = new byte[NameLength];
			f.read(tmp, 0, NameLength);
			Name = new String(tmp, 0, NameLength);

			/**
			 * All fields except name summed are 33 bytes length.
			 * Adding the size of the name, the whole structure must
			 * have a even length. So if the name length is even,
			 * 33 + even = odd
			 * so we skip the padding byte.
			 */
			if (NameLength % 2 == 0) {
				f.skipBytes(1);
			}

			f.seek(Fp + Size);
		} catch (IOException e) {
			System.out.println("IOException occurred while reading the directory record: " + e.getMessage());
		}
	}

	public int getStructureSize() {
		return Size;
	}

	public String getName() {
		return Name;
	}

	public int getSize() {
		return LengthOfData.getLe();
	}

	public int getSector() {
		return FirstSectorOfData.getLe();
	}

	public Boolean isDirectory() {
		return (Flags & 2) != 0;
	}
}
